package com.example.gulimall.product.service.impl;

import com.example.common.constant.ProductConstant;
import com.example.gulimall.product.entity.AttrEntity;
import com.example.gulimall.product.entity.AttrGroupEntity;
import com.example.gulimall.product.service.AttrGroupService;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.service.CategoryService;
import com.example.gulimall.product.vo.AttrGroupRelationVo;
import com.example.gulimall.product.vo.AttrGroupWithAttersVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.example.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.example.gulimall.product.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired private AttrService attrService;
    @Autowired private AttrGroupService attrGroupService;
    @Autowired private CategoryService categoryService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetails(AttrAttrgroupRelationEntity relation) {
        AttrEntity attrEntity = this.attrService.getById(relation.getId());
        relation.setAttrName(Objects.nonNull(attrEntity) ? attrEntity.getAttrName() : null);
        AttrGroupEntity groupEntity = this.attrGroupService.getById(relation.getAttrGroupId());
        relation.setAttrName(Objects.nonNull(groupEntity) ? groupEntity.getAttrGroupName() : null);
        this.baseMapper.insert(relation);
    }

    @Override
    public void saveRelationBatch(List<AttrGroupRelationVo> relationVo) {
        List<AttrAttrgroupRelationEntity> entityList = relationVo.stream().map(
                a -> {
                    AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
                    BeanUtils.copyProperties(a, entity);

                    AttrEntity attrEntity = this.attrService.getById(a.getAttrId());

                    entity.setAttrName(attrEntity.getAttrName());
                    if (attrEntity.getAttrType() == ProductConstant.AttrEnum.TYPE_ATTR_BASE.getCode()) {
                        entity.setGroupName(this.attrGroupService.getById(a.getAttrGroupId()).getAttrGroupName());
                    }
                    entity.setCatelogName(this.categoryService.getById(attrEntity.getCatelogId()).getName());

                    return entity;
                }
        ).collect(Collectors.toList());
        this.saveBatch(entityList);
    }

}